package menu;

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.net.URL;
import java.util.Vector;
import javax.imageio.ImageIO;
import javax.swing.BoxLayout;
import javax.swing.JFileChooser;
import javax.swing.JPanel;
import javax.swing.filechooser.FileFilter;

import arena.Arena;
import utility.MP3Player;
import editor.Editor;
import utility.ExtensionFileFilter;

public class PanelMain extends JPanel implements MouseListener{
	public static MainGameScreen mainScreen;
	public static enum ButtonAction {PLAY,LOAD,EDITOR,OPTIONS,CREDITS,EXIT};
	public static MP3Player bkSound = new MP3Player("src/sound/tron.mp3");
	private static final long 	serialVersionUID = 1L;
	private JPanel 				vertPanel;
	private Vector<ButtonMenu> 	buttons = new Vector<ButtonMenu>();
	private BufferedImage 		layer_0 = LoadImg("textures/game_menu.png");
	private BufferedImage 		layer_1 = LoadImg("textures/credits.png");
	private PanelPlay 			panelPlay = new PanelPlay(this);
	private PanelSetting		panelSetting = new PanelSetting(this);
	private PanelCredits		panelCredits = new PanelCredits(this);
	private JPanel				fakePanel = new JPanel();
	public static Settings		settings = new Settings();
	private static PanelMain	panelMain;
	private boolean 			credits;	
	
	public static PanelMain getInstance()
	{
		return panelMain;
	}
	
	public PanelMain(MainGameScreen parent) throws IOException{
	    super(true); //crea un JPanel con doubleBuffered true
	    mainScreen = parent;
	    this.setLayout(new BorderLayout());
	    this.setName("MAIN PANEL");
	    this.setBorder(null);
	    this.setOpaque(true);
	    
	    credits=false;
	    
	    fakePanel.setOpaque(false);
	    fakePanel.setBorder(null);
	    fakePanel.setPreferredSize(new Dimension(this.getWidth(), 64));
	    this.add(fakePanel,BorderLayout.NORTH);
	    
	    vertPanel = new JPanel(true);
	    vertPanel.setLayout(new BoxLayout(vertPanel, BoxLayout.Y_AXIS));
	    vertPanel.setBorder(null);
	    vertPanel.setOpaque(false);
	    this.add(vertPanel,BorderLayout.EAST);
	    
	    setButton("PLAY");
	    setButton("LOAD");
	    setButton("EDITOR");
	    setButton("OPTIONS");
	    setButton("CREDITS");
	    setButton("EXIT");
	    
	    bkSound.play();
	    PanelMain.panelMain = this;
	    this.addMouseListener(this);
	}

	private void setButton(String name) throws IOException
	{
		ButtonMenu button = new ButtonMenu(name);
	    button.setAction(ButtonAction.valueOf(name.toUpperCase()));
	    buttons.add(button);
		
		vertPanel.add(button);
	}
	
	public void paintComponent(Graphics g){
		super.paintComponent(g);
		int width = this.getWidth();
		int height = this.getHeight();
		if (!credits)
			g.drawImage(layer_0, 0, 0, width, height, null);
		else
			g.drawImage(layer_1, 0, 0, width, height, null);
	}
	
	private BufferedImage LoadImg(String path)
	{
		BufferedImage img;
		URL url = MainGameScreen.class.getClassLoader().getResource(path);
	    try
	    {
	    	System.out.println("FILE: " + url.getPath());
	    	img=ImageIO.read(url);
	    } 
	    catch(Exception e)
	    {
	    	System.out.println("Errore!!!");
	    	img=null;
	    }
	    return img;
	}
	
	public void doAction(ButtonAction action) throws IOException
	{
		switch (action)
		{
			case PLAY: 
			    this.remove(panelSetting);
			    this.remove(panelCredits);
				this.add(panelPlay,BorderLayout.WEST);	
				panelSetting.setVisible(false);
				
					panelPlay.txtName.setText(settings.playerName);
					panelPlay.sldNumber.setValue(settings.playerTot);
					panelPlay.cmbColor.setSelectedIndex(settings.playerColor);
				
				panelPlay.setVisible(true);
				panelPlay.setFocusable(true);
				break;
			case LOAD:
				this.loadMap();
				break;
			case OPTIONS:
				this.remove(panelPlay);
				this.remove(panelCredits);
				this.add(panelSetting,BorderLayout.WEST);
				
					panelSetting.cmbEffects.setSelectedIndex(settings.playerEffects);
					panelSetting.cmbMusic.setSelectedIndex(settings.playerMusic);
					panelSetting.cmbScreen.setSelectedIndex(settings.playerScreen);
				
				panelSetting.setVisible(true);
				panelSetting.setFocusable(true);
				break;
			case EDITOR:
				panelPlay.setVisible(false);
				Editor editor = new Editor();
				editor.setVisible(true);
				break;
			case CREDITS: 
				credits=true;
				vertPanel.setVisible(false);
				repaint();
				this.getGraphics().drawImage(layer_1, 0,0,640,400,null);
				System.out.println(panelCredits.getSize().toString());
			    break;
			case EXIT: 
				bkSound.close();
				mainScreen.dispose(); 
				System.exit(0);
			    break;
		}
	}
	
	private void loadMap() throws IOException
	{
		JFileChooser fc = new JFileChooser();
		fc.setCurrentDirectory(new File("src/levels"));
		fc.setDialogTitle("Load Map");
		fc.setFocusable(true);
		FileFilter filter = (FileFilter) new ExtensionFileFilter("Mappa", new String[] { "dat", "DAT" });
		fc.setFileFilter(filter);
		int f = fc.showOpenDialog(null);
		if (f==JFileChooser.APPROVE_OPTION)
			System.out.println(fc.getSelectedFile().getPath());
			if (fc.getSelectedFile().getPath().endsWith(".dat"))
			{
				String levelPath = fc.getSelectedFile().getName();
				levelPath = levelPath.substring(0, levelPath.length()-4);
				System.out.println("LEVEL: " + levelPath);
				
				PanelMain.bkSound.close();
				
				try {
					new Arena(	levelPath, 
								3,
								(int)Math.random()*5+1,
								settings.playerScreen,
								(settings.playerMusic!=0),
								(settings.playerEffects!=0));
				} catch (IOException e1) {
					e1.printStackTrace();
				} catch (InterruptedException e1) {
					e1.printStackTrace();
				}
			}
		return;
	}

	@Override
	public void mouseClicked(MouseEvent e) {
		System.out.println("CLICKED!!!");
		credits=false;
		vertPanel.setVisible(true);
		repaint();
	}

	@Override
	public void mouseEntered(MouseEvent e) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void mouseExited(MouseEvent e) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void mousePressed(MouseEvent e) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void mouseReleased(MouseEvent e) {
		// TODO Auto-generated method stub
		
	}
}
